package com.gjun.struts.resource;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gjun.struts.bean.GjunsystemPrivilege;
import com.gjun.struts.bean.GjunsystemRole;

public class GjunPrivilegeResourceDAO extends HibernateDaoSupport implements
		GjunPrivilegeResourceIf {

	
	public void savePrivilege(GjunsystemPrivilege privilege) throws Exception {
		this.getHibernateTemplate().save(privilege);
		// 为最高级的管理员增加新权限, 因为他是可以使用所有功能的
		GjunsystemRole role = (GjunsystemRole)this.getHibernateTemplate().get(GjunsystemRole.class,1);
		role.getPrivileges().add(privilege);
	}
	
	
	public List<GjunsystemPrivilege> loadAllRootPrivilege() {
		
		return (List<GjunsystemPrivilege>)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query q = session.createQuery("from GjunsystemPrivilege gp where gp.pid=0 and gp.resourcetype=1");
				// 执行2级缓存...
				return q.list();
			}
			
			
		});
	}

	public List<GjunsystemPrivilege> loadChildPrivilegeByPID(final int pid) {  // 1
		return (List<GjunsystemPrivilege>)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query q = session.createQuery("from GjunsystemPrivilege gp where gp.pid="+pid);
				// 执行2级缓存...
				return q.list();
			}
			
			
		});
	}
	
	
	public List<GjunsystemPrivilege> loadAllPrivilege() {
		// TODO Auto-generated method stub
		return (List<GjunsystemPrivilege>)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query q = session.createQuery("from GjunsystemPrivilege gp ");
				// 执行2级缓存...
				return q.list();
			}
			
			
		});
	}
	
	public List<GjunsystemPrivilege> loadAllPageResourcePrivilege() {
		// TODO Auto-generated method stub
		return (List<GjunsystemPrivilege>)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query q = session.createQuery("from GjunsystemPrivilege gp where gp.pid!=0 and gp.resourcetype=1");
				// 执行2级缓存...
				return q.list();
			}
			
			
		});
	}
	
	public static void main(String[] args) {
		ApplicationContext context = new FileSystemXmlApplicationContext("src/applicationContext.xml");
		//BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
		GjunPrivilegeResourceIf dao = (GjunPrivilegeResourceIf)context.getBean("privilegeDAO");
		List<GjunsystemPrivilege> list = dao.loadAllRootPrivilege();
		for(GjunsystemPrivilege root : list){
			System.out.println(root.getId()+"\t"+root.getLabel()+"\t"+root.getHref());
			List<GjunsystemPrivilege> childs = dao.loadChildPrivilegeByPID(root.getId());
			
			for(GjunsystemPrivilege child : childs){
				System.out.println("\t\t\t"+child.getId()+"\t"+child.getLabel()+"\t"+child.getHref());
			}
		}
	}

}
